
// --------------------  AccountUser --------------------
var $accountUser = {
  firstName:"",
  lastName:"",
  login:"",
  password:"",
  email:"",
  telContact:"",
  date:"",
  notes:"",

  clear: function() {
  	var field = ["firstName","lastName","login","password","email","telContact","date","notes"];
  	for(var i=0; i<field.length; i++)
			this[field[i]] = "";
		dwr.util.setValues(this, this);
  },

  save: function() {
		dwr.util.getValues(this);
		AccountUserImpl.save(this, null);
		this.list();
  },

  rm: function() {
		dwr.util.getValues(this);
		AccountUserImpl.remove(this, null);
		this.list();		

  },

  list: function() {
	    AccountUserImpl.list(function(accounts) {
	      var account;
	      var tableName = ["rsTableAccount","rsTableDataAccount"];

	      document.getElementById(tableName[0]).innerHTML = "";
	      document.getElementById(tableName[1]).innerHTML = "";
	      
	      createTable("title", "", ["<b>Login</b>","<b>Nome</b>","<b>Sobrenome</b>","<b>Data</b>"], ["100px","80px","100px","80px"], tableName);
	      for (var i = 0; i < accounts.length; i++) {
	        account = accounts[i];
	        
					var field = [account.login, account.firstName,account.lastName,account.date];
					var fieldLength = ["100px","80px","100px","80px"];
					createTable("result", account.login, field, fieldLength, tableName, $accountUser );

	      }
	    });
   },
    
  get: function(key, type, func) {

  	if(key == null){
  		key = dwr.util.getValue("login");
  	}
  	dwr.util.setValue("login",key);
    dwr.util.getValues(this);
 		if(func!=null){
			  AccountUserImpl.get(this, func);
		 }else{
			    AccountUserImpl.get(this, function(account) {
							dwr.util.setValues(account);
			
			    });
	    }
  }
    

  };



// --------------------  Device --------------------
var $device = {
  imei : "",
  userId : "",
  model : "",
  visible : "",
  datetime : "",
  note : "",
  login:"",
  descr:"",
  
  load: function(type, object) {
  	var field = ["imei","userId","model","visible","datetime","note","login"];
  	
  	for(var x=0; x<field.length; x++){
	  	if(type == "input=object")
	  		dwr.util.setValue((field[x]+"_device"), object[field[x]]);
	  	if(type == "object=input")
	  		this[field[x]] = dwr.util.getValue(field[x] + "_device");

//  		if(field[x] == "login")
//  			alert("login = " + this.login + " - userId = " + this.userId);
	  		
  	}
  },

  clear: function() {
  
  	var field = ["imei_device","userId_device","model_device","visible_device","datetime_device","note_device","login_device"];
  	for(var i=0; i<field.length; i++)
			this[field[i]] = "";
		dwr.util.setValues(this, this);
  },

  save: function() {
//		dwr.util.getValues(this);
		this.load("object=input");
		DeviceImpl.save(this, null);
		this.list();
  },

  rm: function() {
		dwr.util.getValues(this);
		DeviceImpl.remove(this, null);
		this.list();		

  },

  list: function() {
  		dwr.util.getValues(this);
	    DeviceImpl.list(this, function(devices) {
	      var device;
	      var tableName = ["rsTableDevice","rsTableDataDevice"];
	      var fieldLength = ["110px","100px","60px","50px","80px"];
	      
	      document.getElementById(tableName[0]).innerHTML = "";
	      document.getElementById(tableName[1]).innerHTML = "";
	      
	      createTable("title", "", ["<b>Imei</b>","<b>Login</b>","<b>Modelo</b>","<b>Ativo</b>","<b>Data</b>"], fieldLength, tableName);
	      for (var i = 0; i < devices.length; i++) {
		        device = devices[i];
						var field = [device.imei, device.login, device.model, device.visible, device.datetime];
						
						createTable("result", device.imei, field, fieldLength, tableName, $device);
	      }
	    });
   },

   listDevices: function() {
 		dwr.util.getValues(this);
 		
	    DeviceImpl.list(this, function(devices) {
	      var device;
	      var result = "";
	      
	      for (var i = 0; i < devices.length; i++) {
		        device = devices[i];
				result = result + "<tr><td>"+device.descr+"<br/>"+device.note+"</td></tr>";
	      }
	      document.getElementById("resultElencoDevices").innerHTML = "<table border='1px'>" + result + "</table>"
	    });
  },
  
  get: function(key) {
  	if(key == null){
  		key = dwr.util.getValue("imei_device");
  	}
  	dwr.util.setValue("imei_device",key);
    this.load("object=input");
    //dwr.util.getValues(this);
    DeviceImpl.get(this, function(device) {
				$device.load("input=object", device);    
				//dwr.util.setValues(device);
    });
  }
    
  };

// --------------------  LoggerPoints --------------------
var $loggerPoints = {
  listLoggerPoints : [],
  id : "",
  date : "",
  time : "",
  imei : "",
  latitude : "",
  latitudeDir : "",
  longitude : "",
  longitudeDir : "",
  quality : "",
  speed : "",
  typeService : "",
  

  save: function() {
		dwr.util.getValues(this);
    LoggerPointsImpl.save(this, null);
    alert("SAVE - OK"); 
  },

  get: function() {
  	dwr.util.getValues(this);
    this.name = dwr.util.getValue("name");
    LoggerPointsImpl.get(this, function(points) {
	      document.write("imei = " + points.imei);
    });
  },

  getLastCoord: function(idObject) {
	  	dwr.util.getValues(this);
	    this.login = "";
	    LoggerPointsImpl.getLastCoord(this, function(points) {
		      document.getElementById(idObject).innerHTML = "latitude: " + points.latitude + " longitude: " + points.longitude;
	    });
	  },

   add: function(obj) {
	  this.listLoggerPoints = obj;
  },
  
  list: function() {
  	dwr.util.getValues(this);
    
  	LoggerPointsImpl.list(dwr.util.getValue("imei"), dwr.util.getValue("dateTimeI"), dwr.util.getValue("dateTimeF"), function(loggerPoints) {
  		//createTable2();
  		setListLoggerPoints(loggerPoints);
  		
//  		startInterval();
    });
    
    }

  };

function sleep(milliSeconds){
	var startTime = new Date().getTime(); // get the current time
	while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}


function createTable2(loggerPoints){
    var points;
    var result=""; //Solo per test
    var tableBody = "";
    var tableHeader = "<table id='logger' ><thead><tr><td>date</td><td>latitude</td><td>longitude</td><td>indirizzo</td></tr></thead><tbody>";
    var tableFooter = "</tbody></table>";
    for (var i = 0; i < loggerPoints.length; i++) {
      points = loggerPoints[i];
      tableBody = tableBody + "<tr><td>"+points.date+"</td><td>"+points.latitude+"</td><td>"+points.longitude+"</td><td>"+points.address+"</td></tr>"
    }
    document.getElementById("loggerDIV").innerHTML = tableHeader + tableBody + tableFooter;    
    
    var tableWidgetObj_demo2 = new DHTMLSuite.tableWidget();
		tableWidgetObj_demo2.setTableId('logger');
		tableWidgetObj_demo2.setTableWidth(800);
		tableWidgetObj_demo2.setTableHeight(350);
		tableWidgetObj_demo2.setColumnSort(Array(false,false,false,false));
		tableWidgetObj_demo2.init();  
	  
		startInterval();
}


function createTable(type, key, field, fieldLength, tablesname, object){

	className = (type == "result")?"label":"labelRis";
	tableName = (type == "result")?tablesname[1]:tablesname[0];
	
	var objTable = document.createElement("TABLE");
	var objTheader = document.createElement("THEADER");
	var objTbody = document.createElement("TBODY");
	var objTR = document.createElement("TR");
	objTable.cellspacing="0";
	objTable.style["cursor"]="pointer";
	objTR.align="left";

	if(type != "result"){
		objTable.style["borderStyle"] = "double";
	}else{
		objTR["onmousemove"] = function(){this.style.backgroundColor='#CCCCCC'};
		objTR["onmouseout"] = function(){this.style.backgroundColor='#FFFFFF'};	
		objTR["onclick"] = function(){ object.get(key) };	
	}
	
	for(var i=0; i < field.length; i++){
		var objTD = document.createElement("TD");
		objTD.style["width"]=fieldLength[i];
		objTD.style["font-size"]="14px";
		objTD.className=className;
		objTD.innerHTML = field[i];
		objTR.appendChild(objTD);	
	}
	
	objTable.appendChild(objTR);
	
	document.getElementById(tableName).appendChild(objTable);

}
